/**
 * 
 */
package cn.gov.msa.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.sql.visitor.functions.Substring;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.collection.SharedLibraryService;
import cn.gov.msa.dao.AmanuensisMapper;
import cn.gov.msa.dao.AnchoragDefineMapper;
import cn.gov.msa.dao.ApplyMapper;
import cn.gov.msa.dao.ApplyMaterialAnnexMapper;
import cn.gov.msa.dao.ApplyMaterialMapper;
import cn.gov.msa.dao.AuditAnnexMapper;
import cn.gov.msa.dao.AuditOpinionMapper;
import cn.gov.msa.dao.ControlDefineMapper;
import cn.gov.msa.dao.DeclarationsMapper;
import cn.gov.msa.dao.DeclareItemMapper;
import cn.gov.msa.dao.DictionaryDetailMapper;
import cn.gov.msa.dao.FileMapper;
import cn.gov.msa.dao.FlowTaskUserMapper;
import cn.gov.msa.dao.ForeignRescueMapper;
import cn.gov.msa.dao.ForeignWatersPermitMapper;
import cn.gov.msa.dao.NavigationaidsMapper;
import cn.gov.msa.dao.ProhibitedAreaDefineMapper;
import cn.gov.msa.dao.ProhibitedAreaPermitMapper;
import cn.gov.msa.dao.RecordCertificateFileMapper;
import cn.gov.msa.dao.RecordCertificateListMapper;
import cn.gov.msa.dao.RecordCompanyMapper;
import cn.gov.msa.dao.RecordPersonMapper;
import cn.gov.msa.dao.RecordSafetyWorkInlandMapper;
import cn.gov.msa.dao.RecordSafetyWorkPortMapper;
import cn.gov.msa.dao.RecordShipMapper;
import cn.gov.msa.dao.RouteDefineMapper;
import cn.gov.msa.dao.ScavengLiabiInsCerMapper;
import cn.gov.msa.dao.ShipMapper;
import cn.gov.msa.dao.ShipwrecksWorkMapper;
import cn.gov.msa.dao.TowingWorkMapper;
import cn.gov.msa.dao.WatersSafetyMapper;
import cn.gov.msa.dao.WatersWorkCerMapper;
import cn.gov.msa.dao.WatersWorkMapper;
import cn.gov.msa.dao.WatersWorkXYMapper;
import cn.gov.msa.dao.WorkDeviceMapper;
import cn.gov.msa.dao.WorkspaceDefineMapper;
import cn.gov.msa.dao.YachtClubRecordMapper;
import cn.gov.msa.model.Amanuensis;
import cn.gov.msa.model.AnchoragDefine;
import cn.gov.msa.model.Apply;
import cn.gov.msa.model.AuditAnnex;
import cn.gov.msa.model.AuditOpinion;
import cn.gov.msa.model.ControlDefine;
import cn.gov.msa.model.Declarations;
import cn.gov.msa.model.DeclareItemMaterial;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.model.ForeignRescue;
import cn.gov.msa.model.ForeignWatersPermit;
import cn.gov.msa.model.NavigationMark;
import cn.gov.msa.model.Navigationaids;
import cn.gov.msa.model.Port;
import cn.gov.msa.model.ProhibitedAreaDefine;
import cn.gov.msa.model.ProhibitedAreaPermit;
import cn.gov.msa.model.RecordCompany;
import cn.gov.msa.model.RecordSafetyWorkInland;
import cn.gov.msa.model.RecordSafetyWorkPort;
import cn.gov.msa.model.RouteDefine;
import cn.gov.msa.model.ScavengLiabiInsCer;
import cn.gov.msa.model.Ship;
import cn.gov.msa.model.ShipwrecksWork;
import cn.gov.msa.model.TowingWork;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.model.VSelectAduitOpinion;
import cn.gov.msa.model.VSelectBusinessQuery;
import cn.gov.msa.model.VSelectMaterial;
import cn.gov.msa.model.WatersSafety;
import cn.gov.msa.model.WatersWork;
import cn.gov.msa.model.WatersWorkCer;
import cn.gov.msa.model.WatersWorkXY;
import cn.gov.msa.model.Waterway;
import cn.gov.msa.model.WorkDevice;
import cn.gov.msa.model.WorkspaceDefine;
import cn.gov.msa.model.YachtClubRecord;
import cn.gov.msa.service.ApplyService;
import cn.gov.msa.service.FileService;
import cn.gov.msa.service.PortService;
import cn.gov.msa.service.QueryService;
import cn.gov.msa.service.VerifyService;
import cn.gov.msa.service.WaterwayService;
import cn.gov.msa.service.WorkFlowService;
import cn.gov.msa.util.FileUtil;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

/**
 * @author Administrator
 *
 */

@RestController
@RequestMapping(value = "/business", produces = "text/plain;charset=UTF-8")
public class BusinessController {

	@Autowired
	WorkFlowService workFlowService;
	@Autowired
	AuditAnnexMapper auditAnnexMapper;
	@Autowired
	FileMapper fileMapper;
	@Autowired
	QueryService queryService;

	@Autowired
	ApplyService applyService;
	@Autowired
	RecordCompanyMapper recordCompanyMapper;
	@Autowired
	RecordCertificateFileMapper recordCertificateFileMapper;
	@Autowired
	RecordCertificateListMapper recordCertificateListMapper;
	@Autowired
	FileService fileService;
	@Autowired
	PortService portService;
	@Autowired
	VerifyService verifyService;
	@Autowired
	DictionaryDetailMapper dictionaryDetailMapper;
	@Autowired
	FlowTaskUserMapper flowTaskUserMapper;
	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params, HttpServletRequest req) throws Exception {
		String result = "";

		int pageNum = 1;
		int pageSize = 10;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		
		// 默认查询当前登录账号的所属机构信息
		if("".equals(params.optString("orgcode"))){
			UserDetail userDetail = (UserDetail) req.getSession().getAttribute("userDetail");
			params.put("orgcode", userDetail.getOrgcode());
		}

//		VSelectBusinessQuery vSelectBusinessQuery = (VSelectBusinessQuery) JSONObject.toBean(params, VSelectBusinessQuery.class);
//
//		List<VSelectBusinessQuery> vSelectBusinessQueryList = queryService.selectData(pageNum, pageSize, vSelectBusinessQuery);
//		// 获取分页信息
//		PageInfo page = new PageInfo(vSelectBusinessQueryList);
//
//		JSONArray ja = JSONArray.fromObject(vSelectBusinessQueryList);
		
		Apply apply = (Apply)JSONObject.toBean(params, Apply.class);
		List<Apply> applyList = applyService.selectData(pageNum, pageSize, apply);
		// 获取分页信息
		PageInfo page = new PageInfo(applyList);
		
		JSONArray ja = new JSONArray();
		for (Apply a : applyList) {
			JSONObject jsonApply = JSONObject.fromObject(a);
			jsonApply.put("itemtype", a.getItemtypename());
			
			ja.add(jsonApply);
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);

		result = jsonResult.toString();
		return result;
	}


	@RequestMapping(value = "/getapproveusername")
	public String getApproveUserName(HttpSession session, @RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			String instanceid = param.optString("instanceid");
			
			String auditusername = flowTaskUserMapper.concatUser(instanceid);
			
			if("" != auditusername){
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", auditusername);
				
			}
		} catch (Exception e) {
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/saveForm")
	public String saveForm(HttpSession session, @RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = "";

		try {
			String userID = session.getAttribute("userid").toString();

			System.out.println(param);
			String appId = param.getString("appid");

			List<String> nextUserIDs = new ArrayList<String>();
			nextUserIDs.add("accept1");
			nextUserIDs.add("accept2");
			String instanceId = workFlowService.startFlow("level01nocheck", appId, userID, nextUserIDs, true);
			System.out.println("instanceId>>>" + instanceId);
		} catch (Exception e) {
			result = 2;
			info = "删除异常!";
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	private ApplyMapper applyMapper;
	@Autowired
	private DeclareItemMapper declareItemMapper;

	@RequestMapping(value = "/getapplyinfo")
	public String getApplyInfo(String applyno) throws Exception {

		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			Apply apply = applyMapper.selectByApplyno(applyno);

			if (null != apply) {
				// String itemno = apply.getItemno();
				// DeclareItem declareItem =
				// declareItemMapper.selectByPrimaryKey(itemno);

				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", apply);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	private WatersSafetyMapper watersSafetyMapper;
	@Autowired
	private WatersWorkMapper watersWorkMapper;
	@Autowired
	private RecordSafetyWorkInlandMapper recordSafetyWorkInlandMapper;
	@Autowired
	private ShipwrecksWorkMapper shipwrecksWorkMapper;
	@Autowired
	private TowingWorkMapper towingWorkMapper;
	@Autowired
	private ProhibitedAreaDefineMapper prohibitedAreaDefineMapper;
	@Autowired
	private RouteDefineMapper routeDefineMapper;
	@Autowired
	private ControlDefineMapper controlDefineMapper;
	@Autowired
	private AnchoragDefineMapper anchoragDefineMapper;
	@Autowired
	private WorkspaceDefineMapper workspaceDefineMapper;
	@Autowired
	ScavengLiabiInsCerMapper scavengLiabiInsCerMapper;
	@Autowired
	DeclarationsMapper declarationsMapper;
	@Autowired
	private RecordSafetyWorkPortMapper recordSafetyWorkPortMapper;
	@Autowired
	private ProhibitedAreaPermitMapper prohibitedAreaPermitMapper;
	@Autowired
	private NavigationaidsMapper navigationaidsMapper;
	@Autowired
	private YachtClubRecordMapper yachtClubRecordMapper;
	@Autowired
	private ForeignWatersPermitMapper foreignWatersPermitMapper;
	@Autowired
	private ForeignRescueMapper foreignRescueMapper;
	@Autowired
	private RecordShipMapper recordShipMapper;
	@Autowired
	private RecordPersonMapper recordPersonMapper;

	@RequestMapping(value = "/getbusinessinfo")
	public String getBusinessInfo(String applyid, String itemno, HttpServletRequest request) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			JSONObject jsonBusiness = null;

			switch (itemno) {
			case "1001":
				WatersSafety watersSafety = watersSafetyMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(watersSafety);
				break;
			case "1002":
				WatersWork watersWork = watersWorkMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(watersWork);
				break;
			case "1003":
				WatersWork watersWork1 = watersWorkMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(watersWork1);
				break;
			case "1004":	//外国籍船舶或飞机从事海上搜救审批申请
				ForeignRescue foreignRescue = foreignRescueMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(foreignRescue);
				break;
			case "1005":
				ShipwrecksWork shipwrecksWork = shipwrecksWorkMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(shipwrecksWork);
				break;
			case "1006":
				TowingWork towingWork = towingWorkMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(towingWork);
				break;
			case "1007":
				ProhibitedAreaDefine prohibitedAreaDefine = prohibitedAreaDefineMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(prohibitedAreaDefine);
				break;
			case "1008":
				RouteDefine routeDefine = routeDefineMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(routeDefine);
				break;
			case "1009":
				ControlDefine controlDefine = controlDefineMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(controlDefine);
				break;
			case "1010":
				AnchoragDefine anchoragDefine = anchoragDefineMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(anchoragDefine);
				break;
			case "1011":
				WorkspaceDefine workspaceDefine = workspaceDefineMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(workspaceDefine);
				break;
			case "1012":
				ScavengLiabiInsCer scavengLiabiInsCer = scavengLiabiInsCerMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(scavengLiabiInsCer);
				break;
			case "1013":
				Declarations declarations = declarationsMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(declarations);
				break;
			case "1014":
				ProhibitedAreaPermit prohibitedAreaPermit = prohibitedAreaPermitMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(prohibitedAreaPermit);
				break;
			case "1015":
				RecordSafetyWorkInland recordSafetyWorkInland = recordSafetyWorkInlandMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(recordSafetyWorkInland);
				break;
			case "2001":	// 外国籍船舶进入或临时进入非对外开放水域许可申请
				ForeignWatersPermit foreignWatersPermit = foreignWatersPermitMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(foreignWatersPermit);
				break;
			case "2002":
				RecordSafetyWorkPort recordSafetyWorkPort = recordSafetyWorkPortMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(recordSafetyWorkPort);
				break;
			case "2003":	// 游艇俱乐部备案申请
				YachtClubRecord yachtClubRecord = yachtClubRecordMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(yachtClubRecord);
				break;
			case "9001":
				Navigationaids navigationaids = navigationaidsMapper.selectByPrimaryKey(applyid);
				jsonBusiness = JSONObject.fromObject(navigationaids);
				break;
			case "10001":
				JSONObject recordCompany = JSONObject.fromObject(recordCompanyMapper.selectByApplyid(applyid));
				if(recordCompany!=null&&!recordCompany.isNullObject()) {
					JSONArray recordcertificatelist=recordCertificateListMapper.selectByRecordid(recordCompany.getString("recordid"));
					for (int i = 0; i < recordcertificatelist.size(); i++) {
						if(recordcertificatelist.getJSONObject(i)!=null&&!recordcertificatelist.getJSONObject(i).isNullObject()) {
							JSONArray recordcertificatefile=recordCertificateFileMapper.selectByCertificateid(recordcertificatelist.getJSONObject(i).getString("certificateid"));
							for (int j = 0; recordcertificatefile != null && j < recordcertificatefile.size(); j++) {
								String filename = recordcertificatefile.optJSONObject(j).optString("FILENAME");
								String fileid = recordcertificatefile.optJSONObject(j).optString("FILEID");
								String fileurl = "" +
										"<a href=\"" + request.getContextPath() + "/downFiles.do?fileid=" + fileid +
									    "&filename=" + filename + "\" target=\"_blank\">" + filename + "</a>";
								recordcertificatefile.optJSONObject(j).put("FILEURL", fileurl);
							}
							recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
						}
					}
					recordCompany.put("recordcertificatelist", recordcertificatelist);
				}
				jsonBusiness = JSONObject.fromObject(recordCompany);
				break;
			case "10002":
				JSONObject recordShip = JSONObject.fromObject(recordShipMapper.selectByApplyId(applyid));
				if(recordShip!=null&&!recordShip.isNullObject()) {
					JSONArray recordcertificatelist=recordCertificateListMapper.selectByRecordid(recordShip.getString("recordid"));
					for (int i = 0; i < recordcertificatelist.size(); i++) {
						if(recordcertificatelist.getJSONObject(i)!=null&&!recordcertificatelist.getJSONObject(i).isNullObject()) {
							JSONArray recordcertificatefile=recordCertificateFileMapper.selectByCertificateid(recordcertificatelist.getJSONObject(i).getString("certificateid"));
							recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
						}
					}
					recordShip.put("recordcertificatelist", recordcertificatelist);
				}
				jsonBusiness = JSONObject.fromObject(recordShip);
				break;
			case "10003":
				JSONObject recordPerson = JSONObject.fromObject(recordPersonMapper.selectByApplyId(applyid));
				if(recordPerson!=null&&!recordPerson.isNullObject()) {
					JSONArray recordcertificatelist=recordCertificateListMapper.selectByRecordid(recordPerson.getString("recordid"));
					for (int i = 0; i < recordcertificatelist.size(); i++) {
						if(recordcertificatelist.getJSONObject(i)!=null&&!recordcertificatelist.getJSONObject(i).isNullObject()) {
							JSONArray recordcertificatefile=recordCertificateFileMapper.selectByCertificateid(recordcertificatelist.getJSONObject(i).getString("certificateid"));
							recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
						}
					}
					recordPerson.put("recordcertificatelist", recordcertificatelist);
				}
				jsonBusiness = JSONObject.fromObject(recordPerson);
				break;
			}

			if (null != jsonBusiness) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", jsonBusiness);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	private ApplyMaterialMapper applyMaterialMapper;
	private ApplyMaterialAnnexMapper applyMaterialAnnexMapper;

	@RequestMapping(value = "/getmaterialinfo")
	public String getMaterialInfo(String applyid,HttpServletRequest httpServletRequest) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			// ApplyMaterial ApplyMaterial = new ApplyMaterial();
			// ApplyMaterial.setApplyid(applyid);
			// List<ApplyMaterial> applyMaterialList =
			// applyMaterialMapper.selectData(ApplyMaterial);
			//
			// JSONArray jaMaterial = JSONArray.fromObject(applyMaterialList);

			VSelectMaterial vSelectMaterial = new VSelectMaterial();
			vSelectMaterial.setApplyid(applyid);
			List<VSelectMaterial> vSelectMaterialList = applyMaterialMapper.selectMaterialByApplyid(vSelectMaterial);
			JSONArray jaMaterial = JSONArray.fromObject(vSelectMaterialList);
			
			if (0 < jaMaterial.size()) {
				for (int i = 0; i < jaMaterial.size(); i++) {
//					String fileurl="<a href=\""+httpServletRequest.getContextPath()+"/business/getMaterialbyid.do?fileid="+jaMaterial.getJSONObject(i).getString("fileid")+"\" target=\"_blank\">"+jaMaterial.getJSONObject(i).getString("filename")+"</a>";
//					<a href="/gahCMP/business/getMaterialbyid.do?fileid=7BC8E5710DC933F7E050007F0100191D,7BC8E570DFC401ACE050007F01001921" target="_blank">app2.jpg,app1.png</a>
					String fileurl = "";
					
					String[] fileids = jaMaterial.getJSONObject(i).getString("fileid").split(",");
					String[] filenames = jaMaterial.getJSONObject(i).getString("filename").split(",");
					for(int ifile=0; ifile < fileids.length; ifile++){
						String fileid = fileids[ifile];
						String filename = filenames[ifile];
						
						fileurl += "<a href=\""+httpServletRequest.getContextPath()+"/business/getMaterialbyid.do?fileid="+ fileid+"\" target=\"_blank\">"+filename+"</a>" + '，';
					}
					if(!"".equals(fileurl)){
						fileurl = fileurl.substring(0, fileurl.length() - 1);
					}
					jaMaterial.getJSONObject(i).put("fileurl", fileurl);
				}
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", jaMaterial);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/getMaterialbyid", method = RequestMethod.GET)
	public void getMaterialById(String fileid,HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception {
		try {
			String filename = "";
			byte[] b = null;
			
    		JSONObject jsonParams = new JSONObject();
    		jsonParams.put("fileid", fileid);
    		
    		int result = 0;
    		String info = InfoObjUtil.Error_NODATA;
    	    JSONObject jsonResult = new JSONObject();
    		
    		try{    	       
    	        	fileid = jsonParams.getString("fileid");
					cn.gov.msa.model.File file = fileService.selectByPrimaryKey(fileid);
					filename = file.getFilename() + "." + file.getFilesuffix();
    		        String content = FileUtil.bytesToHexString(file.getStorecontent());
    		        jsonResult.put("datas", content);
    	        	result = 1;
    	        	info = InfoObjUtil.Success_select_OK;
    	        
    		}
    		catch(Exception e){
    			e.printStackTrace();
            	result = 2;
            	info = InfoObjUtil.Exption_msg;
    		}
    		
    		response.setHeader("Content-Disposition",
					"attachment; filename=" + new String(filename.getBytes(), "ISO-8859-1"));
    		
        	jsonResult.put("result", result);
        	jsonResult.put("info", info);
    		if(1 == jsonResult.getInt("result")){
    			String content = jsonResult.getString("datas");
    			
    			b = FileUtil.hexStringToBytes(content);
    			System.out.println("LENGGE");
				OutputStream out = response.getOutputStream();
				out.write(b);
				out.close();
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/getMaterialbyid2", method = RequestMethod.GET)
	public void getMaterialById2(String fileid,HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
		cn.gov.msa.model.File material = fileService.selectByPrimaryKey(fileid);
		if (material != null) {
			byte[] bytes = material.getStorecontent();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			baos.write(bytes); // 把byte写进输出流里
			if (baos != null) {
				httpServletResponse.setContentType(material.getFiletype());
				httpServletResponse.setContentLength(baos.size());
				httpServletResponse.setHeader("Expires", "0");
				httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
				httpServletResponse.setHeader("Pragma", "public");
				httpServletResponse.setHeader("Content-Disposition", "inline;filename=" + new String((material.getFilename() + "."+material.getFilesuffix()).getBytes(), "iso-8859-1"));
				javax.servlet.ServletOutputStream sos = httpServletResponse.getOutputStream();
				baos.writeTo(sos); // byte输出流写入servlet输出流
				sos.flush();
				sos.close();
				baos.close();
			}
		}
	}
	
	
	
	@RequestMapping(value = "/downAllmaterial")
	public void downAllmaterial(String applyid, HttpServletResponse httpServletResponse) throws Exception {

		VSelectMaterial vSelectMaterial = new VSelectMaterial();
		vSelectMaterial.setApplyid(applyid);
		List<VSelectMaterial> vSelectMaterialList = applyMaterialMapper.selectMaterialByApplyid(vSelectMaterial);
		httpServletResponse.setContentType("application/x-msdownload");
		httpServletResponse.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode("批量下载.zip", "utf-8"));
		httpServletResponse.setHeader("Expires", "0");
		httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
		httpServletResponse.setHeader("Pragma", "public");
		
		List<cn.gov.msa.model.File> files = new ArrayList<cn.gov.msa.model.File>();
		for (int i = 0; i < vSelectMaterialList.size(); i++) {
			if (vSelectMaterialList.get(i).getFileid() != null) {
				String[] fileIds = vSelectMaterialList.get(i).getFileid().split(",");
				for (int j = 0; j < fileIds.length; j++) {
					if (!"".equals(fileIds[j])) {
						cn.gov.msa.model.File file = fileMapper.selectByPrimaryKey(fileIds[j]);
						if (file != null && file.getStorecontent() != null) {
							files.add(file);
						}
					}
				}
			}
		}
		
		ZipOutputStream zipStream = new ZipOutputStream(httpServletResponse.getOutputStream());
		BufferedOutputStream bos = new BufferedOutputStream(zipStream);
		try {
			// 构造最终压缩包的输出流
			for (cn.gov.msa.model.File file : files) {

				// 压缩条目不是具体独立的文件，而是压缩包文件列表中的列表项，称为条目，就像索引一样
				ZipEntry zipEntry = new ZipEntry(file.getFilename() + "." + file.getFilesuffix());
				// 定位该压缩条目位置，开始写入文件到压缩包中
				zipStream.putNextEntry(zipEntry);

				// 输入缓冲流
				BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file.getStorecontent()));
				int read = 0;
				// 创建读写缓冲区
				byte[] buf = new byte[1024 * 10];
				while ((read = bis.read(buf, 0, 1024 * 10)) != -1) {
					bos.write(buf, 0, read);
				}
				bis.close();
				bos.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bos.close();
		}

	}

	@RequestMapping(value = "/updateapply")
	public String updateApply(String businessid, String nodecode) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			Apply apply = new Apply();
			apply.setApplyid(businessid);
			apply.setNodeno(nodecode);
			int i = applyMapper.updateByPrimaryKey(apply);

			if (0 < i) {
				result = 1;
				info = InfoObjUtil.Success_save_OK;
			} else {
				result = 0;
				info = InfoObjUtil.Error_save_no;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	AuditOpinionMapper auditOpinionMapper;

	@RequestMapping(value = "/getauditopinion")
	public String getAuditOpinion(String applyid,String instanceid,String pinstanceid) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			VSelectAduitOpinion record = new VSelectAduitOpinion();
			record.setBusinessid(applyid);
			record.setInstanceid(instanceid);
			record.setPinstanceid(pinstanceid);
			List<VSelectAduitOpinion> vSelectAduitOpinionList = auditOpinionMapper.selectData(record);

			if (0 < vSelectAduitOpinionList.size()) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", vSelectAduitOpinionList);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	/**
	 * 获取初审意见
	 * */
	@RequestMapping(value = "/getfirsttrialOpinion")
	public String getFirstTrialOpinion(String applyid) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			AuditOpinion auditOpinion = auditOpinionMapper.selectFirstTrialOpinion(applyid);

			if (null != auditOpinion) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", auditOpinion);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/selectJointlySignOpinion")
	public String selectJointlySignOpinion(@RequestBody JSONObject param) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();
		try {
			JSONArray resultArray = auditOpinionMapper.selectJointlySignOpinion(param);
			if (0 < resultArray.size()) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", resultArray);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/getaduitannex")
	public String getAduitAnnex(String opinionid) throws Exception {

		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		if(!"".equals(opinionid)) {
			try {
				
				AuditAnnex record=new AuditAnnex();
				record.setOpinionid(opinionid);
				List<AuditAnnex> auditAnnexList = auditAnnexMapper.selectData(record);
				if (0 < auditAnnexList.size()) {
					result = 1;
					info = InfoObjUtil.Success_select_OK;
					jsonResult.put("datas", JSONArray.fromObject(auditAnnexList));
				} else {
					result = 0;
					info = InfoObjUtil.Error_NODATA;
				}
			} catch (Exception e) {
				e.printStackTrace();
				result = 2;
				info = InfoObjUtil.Exption_msg;
			}
		}
		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/getcorrectioninfo")
	public String getCorrectionInfo(String applyno) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			String datas = applyService.getCorrectionInfo(applyno);

			if (datas != null) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", datas);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/getacceptinfo")
	public String getAcceptInfo(String applyid) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			String datas = applyService.getAcceptInfo(applyid);

			if (datas != null && !datas.equals("")) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", datas);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/getlastopinion")
	public String getLastOpinion(String applyid) throws Exception {

		int result = 0;
		String info = "";
		JSONObject jsonResult = new JSONObject();

		try {
			AuditOpinion auditOpinion = auditOpinionMapper.selectLastData(applyid);
			String datas = JSONObject.fromObject(auditOpinion).toString();

			if (datas != null) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", datas);
			} else {
				result = 0;
				info = InfoObjUtil.Error_NODATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	AmanuensisMapper amanuensisMapper;

	@RequestMapping(value = "/getamanuensis")
	public String getAmanuensis(String applyid) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			Amanuensis amanuensis = new Amanuensis();
			amanuensis.setApplyid(applyid);

			List<Amanuensis> amanuensisList = applyService.getAmanuensis(amanuensis);
			for (int i = 0; i < amanuensisList.size(); i++) {
				amanuensisList.get(i).setFileurl("/gahCMP/business/getamanuensisbyid.do?amanuensisid=" + amanuensisList.get(i).getAmanuensisid());
			}
			if (amanuensisList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", amanuensisList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		System.err.println(jsonResult);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/getamanuensisbyid", method = RequestMethod.GET)
	public void getAmanuensisById(String amanuensisid, HttpServletResponse httpServletResponse) throws Exception {
		try{
			Amanuensis amanuensis = applyService.getAmanuensisById(amanuensisid);
			if (amanuensis != null) {
				byte[] bytes = amanuensis.getAttachment();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				baos.write(bytes); // 把byte写进输出流里
				if (baos != null) {
					httpServletResponse.setContentType("application/pdf");
					httpServletResponse.setContentLength(baos.size());
					httpServletResponse.setHeader("Expires", "0");
					httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
					httpServletResponse.setHeader("Pragma", "public");
					httpServletResponse.setHeader("Content-Disposition", "inline;filename=" + new String((amanuensis.getAmanuensisname() + ".pdf").getBytes(), "iso-8859-1"));
					javax.servlet.ServletOutputStream sos = httpServletResponse.getOutputStream();
					baos.writeTo(sos); // byte输出流写入servlet输出流
					sos.flush();
					sos.close();
					baos.close();
				}
			}
		}
		catch(Exception e){
			applyService.saveAmanuensisByTask();
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/getannexidbyid", method = RequestMethod.GET)
	public void getAnnexById(String annexid, HttpServletResponse httpServletResponse) throws Exception {
		
		AuditAnnex auditAnnex = auditAnnexMapper.selectByPrimaryKey(annexid);
		if (auditAnnex != null) {
			byte[] bytes = auditAnnex.getStorecontent();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			baos.write(bytes); // 把byte写进输出流里
			if (baos != null) {
				httpServletResponse.setContentType(auditAnnex.getFiletype());
				httpServletResponse.setContentLength(baos.size());
				httpServletResponse.setHeader("Expires", "0");
				httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
				httpServletResponse.setHeader("Pragma", "public");
				httpServletResponse.setHeader("Content-Disposition", "inline;filename=" + new String((auditAnnex.getFilename()).getBytes(), "iso-8859-1"));
				javax.servlet.ServletOutputStream sos = httpServletResponse.getOutputStream();
				baos.writeTo(sos); // byte输出流写入servlet输出流
				sos.flush();
				sos.close();
				baos.close();
			}
		}
	}
	
	
	
	
	// 获取业务的提交材料列表
	@RequestMapping(value = "/getMaterial")
	public String getMaterial(String itemno) throws Exception {

		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		try {

			List<DeclareItemMaterial> declareItemMaterialList = applyService.getMaterial(itemno);
			if (declareItemMaterialList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", declareItemMaterialList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/apply")
	 public String apply(HttpSession session, @RequestBody JSONObject params) {

		String info = InfoObjUtil.Error_save_no;
		JSONObject jsonResult = new JSONObject();
		int result = 0;

		try {
			JSONObject jsonApply = params.getJSONObject("Apply");
			UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
			jsonApply.put("orgcode", userDetail.getOrgcode());
			jsonApply.put("orgname", userDetail.getOrgname());
			jsonApply.put("sourcechannelcode", "03");
			JSONObject jsonBusiness = params.getJSONObject("Business");
			JSONArray jaMaterial = params.optJSONArray("Material");
			JSONArray jaShip = new JSONArray();
			JSONArray jaVerify = params.optJSONArray("Verify");
			if (params.has("Ship")) {
				jaShip = JSONObject.fromObject(params).getJSONArray("Ship");
			}

			JSONArray jaCoordinate = new JSONArray();
			if (params.has("Coordinate")) {
				jaCoordinate = params.getJSONArray("Coordinate");
			}

			JSONArray jaDevice = new JSONArray();
			if (params.has("Device")) {
				jaDevice = params.getJSONArray("Device");
			}
			
			String applyno = applyService.saveData(jsonApply, jsonBusiness, jaMaterial, jaShip, jaCoordinate,jaVerify, jaDevice, true);

			if ("" != applyno) {
				result = 1;
				info = InfoObjUtil.Success_save_OK;

				JSONObject jsonApplyResult = new JSONObject();
				jsonApplyResult.put("applyno", applyno);
				jsonResult.put("datas", jsonApplyResult);

				// 保存文书
				applyService.saveAmanuensis(applyno, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	 }

	@RequestMapping(value = "/getDetail")
	public String getDetail(String applyno) {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			String datas = applyService.getInfo(applyno);

			if (datas != null) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;

				jsonResult.put("datas", datas);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	ShipMapper shipMapper;

	@RequestMapping(value = "/getshipinfo")
	public String getShipInfo(String applyid) {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			List<Ship> shipList = shipMapper.selectByApplyid(applyid);

			if (shipList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;

				jsonResult.put("datas", shipList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@Autowired
	WatersWorkXYMapper watersWorkXYMapper;

	@RequestMapping(value = "/getcoordinateinfo")
	public String getCoordinateInfo(String applyid) {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			List<WatersWorkXY> watersWorkXYList = watersWorkXYMapper.selectByApplyid(applyid);

			if (watersWorkXYList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;

				jsonResult.put("datas", watersWorkXYList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/getCompanyList", method = RequestMethod.GET)
	public String getCompanyList(String companyname, HttpSession session) throws Exception {

		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		String orgcode = userDetail.getOrgcode();
		
		RecordCompany recordCompany = new RecordCompany();
		recordCompany.setCompanyname(companyname);
		recordCompany.setOrgcode(orgcode);
		recordCompany.setState((short) 1);
		List<RecordCompany> companyList = recordCompanyMapper.selectData(recordCompany);
		JSONArray jsonArray = JSONArray.fromObject(companyList);
		for (int i = 0; i < jsonArray.size(); i++) {
			jsonArray.getJSONObject(i).put("id", jsonArray.getJSONObject(i).get("companyname"));
			jsonArray.getJSONObject(i).put("text", jsonArray.getJSONObject(i).get("companyname"));
			
			JSONObject company = jsonArray.getJSONObject(i);
			if (company != null  && !company.isNullObject()) {
				JSONArray recordcertificatelist = recordCertificateListMapper
						.selectByRecordid(company.getString("recordid"));
				for (int j = 0; j < recordcertificatelist.size(); j++) {
					if (recordcertificatelist.getJSONObject(j) != null
							&& !recordcertificatelist.getJSONObject(j).isNullObject()) {
						JSONArray recordcertificatefile = recordCertificateFileMapper.selectByCertificateid(
								recordcertificatelist.getJSONObject(j).getString("certificateid"));
						recordcertificatelist.getJSONObject(j).put("recordcertificatefile", recordcertificatefile);
					}
				}
				jsonArray.getJSONObject(i).put("recordcertificatelist", recordcertificatelist);
			}
		}
		return jsonArray.toString();
	}
	
	/**
	 * 船舶查询
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getshiplist", method = RequestMethod.POST)
	public String queryShip(@RequestBody JSONObject params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			JSONObject jaShip = JSONObject.fromObject(SharedLibraryService.queryShip(params));
			return jaShip.toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
			jsonResult.put("result", result);
			jsonResult.put("info", info);
		}


		return jsonResult.toString();
	}
	
	/**
	 * 泊位查询
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getberthlist", method = RequestMethod.POST)
	public String queryBerth(@RequestBody JSONObject params, HttpSession session) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		params.put("SLJGDM", userDetail.getOrgcode());
		try {
			JSONObject jaBerth = JSONObject.fromObject(SharedLibraryService.queryBerth(params));
			return jaBerth.toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * 港口查询
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getportlist", method = RequestMethod.POST)
	public String queryPort(@RequestBody JSONObject param, HttpSession session) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
	         
			String orgcode = "";
			if(param.has("orgcode")){
				orgcode = param.getString("orgcode");
			} else {
				UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
				orgcode = userDetail.getOrgcode();
			}
			List<Port> portList = portService.selectByOrgcode(orgcode);
	        
			if(portList.size() > 0){
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				
				jsonResult.put("datas", portList);
			}
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}
	
	@Autowired
	WaterwayService waterwayService;
	/**
	 * 水道查询
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getwaterway", method = RequestMethod.POST)
	public String waterwayData(String orgcode, HttpSession session) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		try {
			Waterway waterway = new Waterway();
			waterway.setOrgcode("060100");

			List<Waterway> waterwayList = waterwayService.getWaterwayList(waterway);

			if (waterwayList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", waterwayList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * 航标查询
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getmark", method = RequestMethod.GET)
	public String markData(String waterwaycode) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			NavigationMark navigationMark = new NavigationMark();
			navigationMark.setWaterwaycode(waterwaycode);

			List<NavigationMark> navigationMarkList = waterwayService.getMarkList(navigationMark);

			if (navigationMarkList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", navigationMarkList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * 校验数据查询
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getverify")
	public String getVerify(@RequestBody JSONObject params,HttpServletRequest httpServletRequest) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			String applyid = params.getString("applyid");
			JSONArray ja = params.getJSONArray("shipidnumber");
			List list = new ArrayList();
			for(Object object : ja){
				list.add(verifyService.getVerifyShip(applyid,object.toString()));
			}
			if (list.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	

	@Autowired
	WatersWorkCerMapper watersWorkCerMapper;
	
	@RequestMapping(value = "/makingcertificate", method = RequestMethod.POST)
	public String makingCertificate(@RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = "证书生成失败！";
		JSONObject jsonResult = new JSONObject();

		try {
			String applyid = "";
			if(param.has("applyid")){
				applyid = param.optString("applyid");

				Apply apply = applyMapper.selectByPrimaryKey(applyid);
				String orgcode= apply.getOrgcode();

				WatersWorkCer watersWorkCer = watersWorkCerMapper.selectByPrimaryKey(applyid);
				
				int iSave = 0;


				JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd"}));
				WatersWorkCer record = (WatersWorkCer)JSONObject.toBean(param, WatersWorkCer.class);
				if(null == watersWorkCer){
					String certificatenumber = watersWorkMapper.setCertificateNo(orgcode);
					
					if(certificatenumber != null && !orgcode.isEmpty()){
				        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
				        Date date = new Date();
				        String certificateyear = sdf.format(date);
				        
				        record.setApplyid(applyid);
				        record.setCertificateyear(certificateyear);
				        record.setCertificatenumber(certificatenumber);
				        iSave = watersWorkCerMapper.insert(record);
					}
				}
				else{
					iSave = watersWorkCerMapper.updateByPrimaryKey(record);
				}
				
				if(iSave > 0){
	//				String rulenum = "222268A68575DD3B";
					String rulenum = null;
					
					String applyno= apply.getApplyno();
					
					WatersWork watersWork = watersWorkMapper.selectByPrimaryKey(applyid);
					boolean bSaveAmanuensis = applyService.saveAmanuensis(applyno, rulenum);   //生成PDF
					
					if(bSaveAmanuensis){
						result = 1;
						info = "证书重新成功！";
					}
				}
			}
			
			

		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(String applyno) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_save_no;
		JSONObject jsonResult = new JSONObject();

		try {
			Apply record = applyMapper.selectByApplyno(applyno);
			
			record.setApplyno(applyno);
			record.setIsdelete(1);
			
			int apply = applyMapper.updateByPrimaryKeySelective(record);
			
			if(apply > 0){
				result = 1;
				info = "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/createAmanuensis.do", method = RequestMethod.GET)
	public String createAmanuensis(String amanuensisid, HttpServletResponse httpServletResponse) throws Exception {
		int result = 1;
		String info = InfoObjUtil.Success_save_OK;
		JSONObject jsonResult = new JSONObject();
		try{
			applyService.saveAmanuensisByTask();
		} catch(Exception e){
			result = 2;
			info = e.getMessage();
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	@Autowired
	WorkDeviceMapper workDeviceMapper;
	
	@RequestMapping(value = "/getworkdevice.do", method = RequestMethod.GET)
	public String getWorkDevice(String applyid) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		JSONObject jsonDevice = new JSONObject();
		try {
			List<WorkDevice> devices = workDeviceMapper.selectByApplyid(applyid);

			if (devices.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonDevice.put("devices", devices);
				jsonResult.put("datas", jsonDevice);
			}
			
			List<Ship> shipList = shipMapper.selectByApplyid(applyid);

			if (shipList.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonDevice.put("ships", shipList);
				jsonResult.put("datas", jsonDevice);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
		
	}
	


	@RequestMapping(value = "/getwatersworkcerinfo.do", method = RequestMethod.GET)
	public String getWatersWorkCerInfo(String applyid) throws Exception {
		int result = 0;
		String info = "没找到相关证书！";
		JSONObject jsonResult = new JSONObject();
		try{
			
			Map map = watersWorkCerMapper.selectCerInfoByPrimaryKey(applyid);
			
			
			if(null != map){
				result = 1;
				info  = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", map);
			}
			
		} catch(Exception e){
			result = 2;
			info = InfoObjUtil.Exption_msg;
			e.printStackTrace();
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
}
